using System;
using System.Collections.Generic;
using System.Linq;
namespace TemplateWeb.Controllers
{
    #region using
    using System;
    using System.Web.Mvc;
    using System.Web.Security;
    using System.Collections.Generic;
    using TemplateWeb.Models.ViewModels;
    using TemplateWeb.Models.Repositories;
    using TemplateWeb.Helpers;
    using TemplateWeb.Models;
    using System.IO;
    using System.Web;
    using TemplateWeb.ShoppingCartModule;
    #endregion
    public abstract class BaseController : Controller
    {
        protected ThongTinCongTyRepository _repThongTinCongTy = new ThongTinCongTyRepository();
        protected TinTucRepository _repTinTuc = new TinTucRepository();
        protected SachRepository _repSach = new SachRepository();
        protected PhanHoiRepository _repPhanHoi = new PhanHoiRepository();
        protected LoaiTinTucRepository _repLoaiTinTuc = new LoaiTinTucRepository();
        protected LoaiSachRepository _repLoaiSach = new LoaiSachRepository();
        protected HopThuRepository _repHopThu = new HopThuRepository();
        protected GocChiaSeRepository _repGocChiaSe = new GocChiaSeRepository();
        protected DoiTacRepository _repDoiTac = new DoiTacRepository();
        protected DanhGiaRepository _repDanhGia = new DanhGiaRepository();
        protected NguoiDungRepository _repNguoiDung = new NguoiDungRepository();
        protected GioHangRepository _repCart = new GioHangRepository();
        protected DonDatHangRepository _repDonDatHang = new DonDatHangRepository();
        protected ChiTietDonDatHangRepository _repChiTietDonDatHang = new ChiTietDonDatHangRepository();

        protected string RenderPartialViewToString()
        {
            return RenderPartialViewToString(null, null);
        }

        protected string RenderPartialViewToString(string viewName)
        {
            return RenderPartialViewToString(viewName, null);
        }

        protected string RenderPartialViewToString(string viewName, object model)
        {
            if (string.IsNullOrEmpty(viewName))
                viewName = ControllerContext.RouteData.GetRequiredString("action");
            ViewData.Model = model;
            using (StringWriter sw = new StringWriter())
            {
                ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
                ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
                viewResult.View.Render(viewContext, sw);
                return sw.GetStringBuilder().ToString();
            }
        }


        public GroupViewModel CreateViewModel(DataTransferViewModel dataTransfer)
        {
            GroupViewModel viewModel = new GroupViewModel();
            if (dataTransfer.EnumViewModelType.ToString().Contains("ADMIN"))
            {
                viewModel.LoaiSachModel = RetrieveLoaiSachViewModel(dataTransfer);
                viewModel.LoaiTinTucModel = RetrieveLoaiTinTucViewModel(dataTransfer);
            }
            else if (dataTransfer.EnumViewModelType.ToString().Contains("HOME"))
            {
                viewModel.ThongTinCongTyModel = RetrieveThongTinCongTyViewModel(dataTransfer);
                viewModel.LoaiSachModel = RetrieveLoaiSachViewModel(dataTransfer);
                viewModel.LoaiTinTucModel = RetrieveLoaiTinTucViewModel(dataTransfer);
                viewModel.DoiTacModel = RetrieveDoiTacViewModel(dataTransfer);
                viewModel.TinTucNewestModel = RetrieveTinTucNewestViewModel(dataTransfer);
                viewModel.SachBestSellerModel = RetrieveSachBestSellerViewModel(dataTransfer);
                viewModel.AllSachModel = RetrieveAllSachViewModel(dataTransfer);

                // Add it to the shopping cart 
                var cart = ShoppingCart.GetCart(this.HttpContext);
                // Set up shoppingCart ViewModel
                viewModel.ShoppingCartModel = new GioHangViewModel
                {
                    CartItems = cart.GetCartItems(),
                    CartTotal = cart.GetTotal()
                };

                if (User.Identity.IsAuthenticated)
                {
                    dataTransfer.UserName = User.Identity.Name;
                    viewModel.UsersModel = RetrieveDetailOfNguoiDungByUserNameViewModel(dataTransfer);
                }
            }
            switch (dataTransfer.EnumViewModelType)
            {
                case EnumViewModel.ADMIN_DETAILOF_NGUOIDUNG:
                    viewModel.NguoiDungModel = RetrieveDetailOfNguoiDungViewModel(dataTransfer);
                    break;
                case EnumViewModel.ADMIN_DETAILOF_THONGTINCONGTY:
                    viewModel.ThongTinCongTyModel = RetrieveDetailOfThongTinCongTyViewModel(dataTransfer);
                    break;
                case EnumViewModel.ADMIN_DETAILOF_TINTUC:
                    viewModel.TinTucModel = RetrieveDetailOfTinTucViewModel(dataTransfer);
                    break;
                case EnumViewModel.ADMIN_DETAILOF_SACH:
                    viewModel.SachModel = RetrieveDetailOfSachViewModel(dataTransfer);
                    break;
                case EnumViewModel.ADMIN_DETAILOF_PHANHOI:
                    viewModel.PhanHoiModel = RetrieveDetailOfPhanHoiViewModel(dataTransfer);
                    break;
                case EnumViewModel.ADMIN_DETAILOF_HOPTHU:
                    break;
                case EnumViewModel.ADMIN_DETAILOF_GOCCHIASE:
                    viewModel.GocChiaSeModel = RetrieveDetailOfGocChiaSeViewModel(dataTransfer);
                    break;
                case EnumViewModel.ADMIN_DETAILOF_DOITAC:
                    viewModel.DoiTacModel = RetrieveDetailOfDoiTacViewModel(dataTransfer);
                    break;
                case EnumViewModel.ADMIN_NGUOIDUNG:
                    viewModel.NguoiDungModel = RetrieveNguoiDungViewModel(dataTransfer);
                    break;
                case EnumViewModel.ADMIN_THONGTINCONGTY:
                    viewModel.ThongTinCongTyModel = RetrieveThongTinCongTyViewModel(dataTransfer);
                    break;
                case EnumViewModel.ADMIN_TINTUC:
                    viewModel.TinTucModel = RetrieveTinTucViewModel(dataTransfer);
                    break;
                case EnumViewModel.ADMIN_SACH:
                    viewModel.SachModel = RetrieveSachViewModel(dataTransfer);
                    break;
                case EnumViewModel.ADMIN_PHANHOI:
                    viewModel.PhanHoiModel = RetrievePhanHoiViewModel(dataTransfer);
                    viewModel.HopThuModel = RetrieveHopThuViewModel(dataTransfer);
                    break;
                case EnumViewModel.ADMIN_LOAITINTUC:
                    break;
                case EnumViewModel.ADMIN_LOAISACH:
                    break;
                case EnumViewModel.ADMIN_HOPTHU:
                    viewModel.HopThuModel = RetrieveHopThuViewModel(dataTransfer);
                    break;
                case EnumViewModel.ADMIN_GOCCHIASE:
                    viewModel.GocChiaSeModel = RetrieveGocChiaSeViewModel(dataTransfer);
                    break;
                case EnumViewModel.ADMIN_DOITAC:
                    viewModel.DoiTacModel = RetrieveDoiTacViewModel(dataTransfer);
                    break;
                case EnumViewModel.ADMIN_DANHGIA:
                    break;
                case EnumViewModel.ADMIN_ORDERS:
                    viewModel.DonDatHangModel = RetrieveOrdersViewModel(dataTransfer);
                    break;
                case EnumViewModel.ADMIN_ORDERS_REVENUE_EXECUTE:
                    viewModel.DonDatHangModel = RetrieveOrdersRevenueViewModel(dataTransfer);
                    break;
                case EnumViewModel.ADMIN_ORDERDETAIL:
                    viewModel.DonDatHangModel = RetrieveOrdersRevenueViewModel(dataTransfer);
                    viewModel.SelectedDonDatHangModel = RetrieveDetailOfDonDatHangViewModel(dataTransfer);
                    viewModel.ChiTietDonDatHangModel = RetrieveChiTietDonDatHangViewModel(dataTransfer);
                    break;
                case EnumViewModel.ADMIN_ORDERDETAIL_EDIT:
                    viewModel.DonDatHangModel = RetrieveOrdersViewModel(dataTransfer);
                    viewModel.SelectedDonDatHangModel = RetrieveDetailOfDonDatHangViewModel(dataTransfer);
                    viewModel.ChiTietDonDatHangModel = RetrieveChiTietDonDatHangViewModel(dataTransfer);
                    break;
                case EnumViewModel.ADMIN_TIMKIEMDONHANG_EXECUTE:
                    viewModel.DonDatHangModel = SearchDonDatHang(dataTransfer);
                    break;
                case EnumViewModel.ADMIN_TIMKIEMDONHANG_SELECT:
                    viewModel.DonDatHangModel = SearchDonDatHang(dataTransfer);
                    viewModel.SelectedDonDatHangModel = RetrieveDetailOfDonDatHangViewModel(dataTransfer);
                    viewModel.ChiTietDonDatHangModel = RetrieveChiTietDonDatHangViewModel(dataTransfer);
                    break;
                case EnumViewModel.ADMIN_TIMKIEMORDERDETAIL:
                    viewModel.DonDatHangModel = SearchDonDatHang(dataTransfer);
                    viewModel.SelectedDonDatHangModel = RetrieveDetailOfDonDatHangViewModel(dataTransfer);
                    viewModel.ChiTietDonDatHangModel = RetrieveChiTietDonDatHangViewModel(dataTransfer);
                    break;
                case EnumViewModel.HOME_REGISTER:
                    break;
                case EnumViewModel.HOME_DETAILOF_NGUOIDUNG:
                    viewModel.NguoiDungModel = RetrieveDetailOfNguoiDungByUserNameViewModel(dataTransfer);
                    break;
                case EnumViewModel.HOME_INDEX:
                    viewModel.SachModel = RetrieveSachViewModel(dataTransfer);
                    viewModel.GocChiaSeModel = RetrieveGocChiaSeViewModel(dataTransfer);
                    break;
                case EnumViewModel.HOME_CHITIETSACH:
                    viewModel.SachModel = RetrieveDetailOfSachViewModel(dataTransfer);
                    break;
                case EnumViewModel.HOME_CHITIETGOCCHIASE:
                    viewModel.GocChiaSeModel = RetrieveDetailOfGocChiaSeViewModel(dataTransfer);
                    break;
                case EnumViewModel.HOME_SACH:
                    viewModel.SachModel = RetrieveSachViewModel(dataTransfer);
                    viewModel.DetaifOfLoaiSachModel = RetrieveDetailOfLoaiSachViewModel(dataTransfer);
                    break;
                case EnumViewModel.HOME_TINTUC:
                    viewModel.TinTucModel = RetrieveTinTucViewModel(dataTransfer);
                    viewModel.DetailOfLoaiTinTucModel = RetrieveDetailOfLoaiTinTucViewModel(dataTransfer);
                    break;
                case EnumViewModel.HOME_CHITIETTINTUC:
                    viewModel.TinTucModel = RetrieveDetailOfTinTucViewModel(dataTransfer);
                    break;
                case EnumViewModel.HOME_GOCCHIASE:
                    viewModel.GocChiaSeModel = RetrieveGocChiaSeViewModel(dataTransfer);
                    break;
                case EnumViewModel.HOME_DOITAC:
                    viewModel.DoiTacModel = RetrieveDoiTacViewModel(dataTransfer);
                    break;
                case EnumViewModel.HOME_CHITIETDOITAC:
                    viewModel.DetailOfDoiTacModel = RetrieveDetailOfDoiTacViewModel(dataTransfer);
                    break;
                case EnumViewModel.HOME_THEMCHIASE:
                    break;
                case EnumViewModel.HOME_ORDERHISTORY:
                    viewModel.DonDatHangModel = RetrieveDonDatHangViewModel(dataTransfer);
                    break;
                case EnumViewModel.HOME_ORDERHISTORY_ORDERDETAIL:
                    viewModel.DonDatHangModel = RetrieveDonDatHangViewModel(dataTransfer);
                    viewModel.SelectedDonDatHangModel = RetrieveDetailOfDonDatHangViewModel(dataTransfer);
                    viewModel.ChiTietDonDatHangModel = RetrieveChiTietDonDatHangViewModel(dataTransfer);
                    break;
                case EnumViewModel.HOME_TIMKIEM:
                    viewModel.SachModel = SearchSach(dataTransfer);
                    break;
            }
            return viewModel;
        }

        public bool UploadFile(HttpPostedFileBase file, string path, string name, ref string refName)
        {
            try
            {
                if (!System.IO.File.Exists(Server.MapPath(path + file.FileName)))
                {
                    FileInfo f = new FileInfo(file.FileName);
                    long ngay = DateTime.Now.ToBinary();
                    refName = name + ngay + f.Extension;
                    string fullPath = path + refName;
                    if (file == null)
                        return false;
                    byte[] buffer = new byte[file.ContentLength];
                    file.InputStream.Read(buffer, 0, file.ContentLength);
                    FileStream newFile = new FileStream(Server.MapPath(fullPath), FileMode.Create, FileAccess.Write);
                    newFile.Write(buffer, 0, file.ContentLength);
                    newFile.Close();
                }
                else
                {
                    refName = file.FileName;
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public void DeleteFile(string filePath)
        {
            FileInfo file = new FileInfo(filePath);
            if (file.Exists)
            {
                file.Delete();
            }
        }

        public bool SendingMail(string subject, string emailContent, string to)
        {
            bool result = false;
            try
            {
                var thongtincongty = RetrieveThongTinCongTyViewModel(new DataTransferViewModel());
                if (thongtincongty.GetModel.LstObjModel.Count > 0)
                {
                    WebConfiguration.EmailLienHe = thongtincongty.GetModel.LstObjModel[0].EmailCongTy;
                    WebConfiguration.PasswordEmailLienHe = Encryption.Decrypt(thongtincongty.GetModel.LstObjModel[0].MatKhauEmailCongTy);
                    emailContent = emailContent.Replace("[EmailLienHe]", WebConfiguration.EmailLienHe);

                    System.Net.Mail.SmtpClient mailclient = new System.Net.Mail.SmtpClient();
                    System.Net.NetworkCredential auth = new System.Net.NetworkCredential(WebConfiguration.EmailLienHe, WebConfiguration.PasswordEmailLienHe);
                    mailclient.Host = WebConfiguration.MailServer;
                    mailclient.EnableSsl = true;
                    mailclient.UseDefaultCredentials = true;
                    mailclient.Credentials = auth;
                    System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage(WebConfiguration.EmailLienHe, to);
                    mm.Subject = subject;
                    mm.IsBodyHtml = true;
                    mm.Body = emailContent;
                    mailclient.Send(mm);
                    result = true;
                }
            }
            catch
            {
                result = false;
            }
            return result;
        }

        public DonDatHangViewModel SearchDonDatHang(DataTransferViewModel dataTransfer)
        {
            var lst = _repDonDatHang.Search(dataTransfer.TimKiem_DonDatHang_IDKhachHang, dataTransfer.TimKiem_DonDatHang_DaXacNhan, dataTransfer.TimKiem_DonDatHang_DaGiaoHang, dataTransfer.TimKiem_DonDatHang_DaHuy, dataTransfer.TimKiem_DonDatHang_KhuVuc);
            var totalitem = lst.Count;

            return new DonDatHangViewModel
            {
                GetModel = new GetDonDatHangViewModel
                {
                    LstObjModel = lst,
                    TotalItem = totalitem,
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddDonDatHangViewModel
                {
                },
                EditModel = new EditDonDatHangViewModel
                {
                    ID = dataTransfer.IntID,
                },
                ReferKeys = new DonDatHangReferKeys()
                {
                },
                InfoText = dataTransfer.InfoText
            };
        }

        public SachViewModel SearchSach(DataTransferViewModel dataTransfer)
        {
            var lst = _repSach.Search(dataTransfer.TimKiem_TenSach, dataTransfer.TimKiem_TenTacGia);
            var totalitem = lst.Count;

            return new SachViewModel
            {
                GetModel = new GetSachViewModel
                {
                    LstObjModel = lst,
                    TotalItem = totalitem,
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddSachViewModel
                {
                },
                EditModel = new EditSachViewModel
                {
                    ID = dataTransfer.IntID,
                },
                ReferKeys = new SachReferKeys()
                {
                },
                InfoText = dataTransfer.InfoText
            };
        }

        public DonDatHangViewModel RetrieveOrdersViewModel(DataTransferViewModel dataTransfer)
        {
            var lst = _repDonDatHang.SelectAll().OrderByDescending(n => n.NgayDatHang).ToList();
            var totalitem = lst.Count;

            return new DonDatHangViewModel
            {
                GetModel = new GetDonDatHangViewModel
                {
                    LstObjModel = lst,
                    TotalItem = totalitem,
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddDonDatHangViewModel
                {
                },
                EditModel = new EditDonDatHangViewModel
                {
                    ID = dataTransfer.IntID,
                },
                ReferKeys = new DonDatHangReferKeys()
                {
                    From = dataTransfer.From,
                    To = dataTransfer.To
                },
                InfoText = dataTransfer.InfoText
            };
        }

        public DonDatHangViewModel RetrieveOrdersRevenueViewModel(DataTransferViewModel dataTransfer)
        {
            var lst = new List<DonDatHang>();
            var lstOrder = _repDonDatHang.SelectAll().OrderByDescending(n => n.NgayDatHang);

            foreach (var item in lstOrder)
            {
                if (dataTransfer.From.Date <= item.NgayDatHang.Value.Date && item.NgayDatHang.Value.Date <= dataTransfer.To.Date)
                {
                    lst.Add(item);
                }
            }
            var totalitem = lst.Count;

            return new DonDatHangViewModel
            {
                GetModel = new GetDonDatHangViewModel
                {
                    LstObjModel = lst,
                    TotalItem = totalitem,
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddDonDatHangViewModel
                {
                },
                EditModel = new EditDonDatHangViewModel
                {
                    ID = dataTransfer.IntID,
                },
                ReferKeys = new DonDatHangReferKeys()
                {
                    From = dataTransfer.From,
                    To = dataTransfer.To
                },
                InfoText = dataTransfer.InfoText
            };
        }

        public ChiTietDonDatHangViewModel RetrieveChiTietDonDatHangViewModel(DataTransferViewModel dataTransfer)
        {
            List<ChiTietDonDatHang> lst = null;
            int totalitem = -1;
            if (dataTransfer.IDDonDatHang != -1)
            {
                lst = _repChiTietDonDatHang.SelectByIDDonDatHang(dataTransfer.IDDonDatHang);
                totalitem = lst.Count;
            }
            else if (dataTransfer.IDSach != -1)
            {
                lst = _repChiTietDonDatHang.SelectByIDSach(dataTransfer.IDSach);
                totalitem = lst.Count;
            }
            else
            {
                lst = _repChiTietDonDatHang.SelectAll();
                totalitem = lst.Count;
            }
            return new ChiTietDonDatHangViewModel
            {
                GetModel = new GetChiTietDonDatHangViewModel
                {
                    LstObjModel = lst,
                    TotalItem = totalitem,
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddChiTietDonDatHangViewModel
                {
                },
                EditModel = new EditChiTietDonDatHangViewModel
                {
                    ID = dataTransfer.IntID,
                },
                ReferKeys = new ChiTietDonDatHangReferKeys()
                {
                    IDDonDatHang = dataTransfer.IDDonDatHang,
                    IDSach = dataTransfer.IDSach,
                },
                InfoText = dataTransfer.InfoText
            };
        }

        public DonDatHangViewModel RetrieveDetailOfDonDatHangViewModel(DataTransferViewModel dataTransfer)
        {
            var lst = _repDonDatHang.RetrieveByID(dataTransfer.IDDonDatHang);
            var totalitem = lst.Count;

            return new DonDatHangViewModel
            {
                GetModel = new GetDonDatHangViewModel
                {
                    LstObjModel = lst,
                    TotalItem = totalitem,
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddDonDatHangViewModel
                {
                },
                EditModel = new EditDonDatHangViewModel
                {
                },
                ReferKeys = new DonDatHangReferKeys()
                {
                }
            };
        }

        public DonDatHangViewModel RetrieveDonDatHangViewModel(DataTransferViewModel dataTransfer)
        {
            List<DonDatHang> lst = null;
            int totalitem = -1;

            if (dataTransfer.GuidID.ToString() != "11112222-1111-1111-1111-111122223333")
            {
                lst = _repDonDatHang.SelectByIDUsers(dataTransfer.GuidID);
            }
            else
            {
                lst = _repDonDatHang.SelectAll();
            }

            totalitem = lst.Count;

            return new DonDatHangViewModel
            {
                GetModel = new GetDonDatHangViewModel
                {
                    LstObjModel = lst,
                    TotalItem = totalitem,
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddDonDatHangViewModel
                {
                },
                EditModel = new EditDonDatHangViewModel
                {
                    ID = dataTransfer.IntID,
                },
                ReferKeys = new DonDatHangReferKeys()
                {
                    IDUser = dataTransfer.GuidID
                },
                InfoText = dataTransfer.InfoText
            };
        }

        public ThongTinCongTyViewModel RetrieveThongTinCongTyViewModel(DataTransferViewModel dataTransfer)
        {
            return new ThongTinCongTyViewModel
            {
                GetModel = new GetThongTinCongTyViewModel
                {
                    LstObjModel = _repThongTinCongTy.SelectAll(),
                    TotalItem = _repThongTinCongTy.GetTotalItem(),
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddThongTinCongTyViewModel
                {
                },
                EditModel = new EditThongTinCongTyViewModel
                {
                    ID = dataTransfer.IntID,
                },
                InfoText = dataTransfer.InfoText
            };
        }

        public TinTucViewModel RetrieveTinTucNewestViewModel(DataTransferViewModel dataTransfer)
        {
            List<TinTuc> lst = _repTinTuc.SelectNewest();
            int totalitem = lst.Count;

            return new TinTucViewModel
            {
                GetModel = new GetTinTucViewModel
                {
                    LstObjModel = lst,
                    TotalItem = totalitem,
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddTinTucViewModel
                {
                },
                EditModel = new EditTinTucViewModel
                {
                    ID = dataTransfer.IntID,
                },
                ReferKeys = new TinTucReferKeys()
                {
                    IDLoaiTinTuc = dataTransfer.IDLoaiTinTuc,
                },
                InfoText = dataTransfer.InfoText
            };
        }

        public TinTucViewModel RetrieveTinTucViewModel(DataTransferViewModel dataTransfer)
        {
            List<TinTuc> lst = null;
            int totalitem = -1;
            if (dataTransfer.IDLoaiTinTuc != -1)
            {
                lst = _repTinTuc.SelectByIDLoaiTinTuc(dataTransfer.IDLoaiTinTuc);
                totalitem = _repTinTuc.GetTotalItemByIDLoaiTinTuc(dataTransfer.IDLoaiTinTuc);
            }
            else
            {
                lst = _repTinTuc.SelectAll();
                totalitem = lst.Count;
            }
            return new TinTucViewModel
            {
                GetModel = new GetTinTucViewModel
                {
                    LstObjModel = lst,
                    TotalItem = totalitem,
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddTinTucViewModel
                {
                },
                EditModel = new EditTinTucViewModel
                {
                    ID = dataTransfer.IntID,
                },
                ReferKeys = new TinTucReferKeys()
                {
                    IDLoaiTinTuc = dataTransfer.IDLoaiTinTuc,
                },
                InfoText = dataTransfer.InfoText
            };
        }

        public SachViewModel RetrieveSachBestSellerViewModel(DataTransferViewModel dataTransfer)
        {
            List<Sach> lst = _repSach.SelectBestSeller();
            int totalitem = lst.Count;

            return new SachViewModel
            {
                GetModel = new GetSachViewModel
                {
                    LstObjModel = lst,
                    TotalItem = totalitem,
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddSachViewModel
                {
                },
                EditModel = new EditSachViewModel
                {
                    ID = dataTransfer.IntID,
                },
                ReferKeys = new SachReferKeys()
                {
                    IDLoaiSach = dataTransfer.IDLoaiSach,
                },
                InfoText = dataTransfer.InfoText
            };
        }

        public SachViewModel RetrieveSachViewModel(DataTransferViewModel dataTransfer)
        {
            List<Sach> lst = null;
            int totalitem = -1;
            if (dataTransfer.IDLoaiSach != -1)
            {
                lst = _repSach.SelectByIDLoaiSach(dataTransfer.IDLoaiSach);
                totalitem = _repSach.GetTotalItemByIDLoaiSach(dataTransfer.IDLoaiSach);
            }
            else
            {
                lst = _repSach.SelectAll();
                totalitem = lst.Count;
            }
            return new SachViewModel
            {
                GetModel = new GetSachViewModel
                {
                    LstObjModel = lst,
                    TotalItem = totalitem,
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddSachViewModel
                {
                },
                EditModel = new EditSachViewModel
                {
                    ID = dataTransfer.IntID,
                },
                ReferKeys = new SachReferKeys()
                {
                    IDLoaiSach = dataTransfer.IDLoaiSach,
                },
                InfoText = dataTransfer.InfoText
            };
        }

        public SachViewModel RetrieveAllSachViewModel(DataTransferViewModel dataTransfer)
        {
            List<Sach> lst = _repSach.SelectAll();
            int totalitem = lst.Count;

            return new SachViewModel
            {
                GetModel = new GetSachViewModel
                {
                    LstObjModel = lst,
                    TotalItem = totalitem,
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddSachViewModel
                {
                },
                EditModel = new EditSachViewModel
                {
                    ID = dataTransfer.IntID,
                },
                ReferKeys = new SachReferKeys()
                {
                    IDLoaiSach = dataTransfer.IDLoaiSach,
                },
                InfoText = dataTransfer.InfoText
            };
        }

        public PhanHoiViewModel RetrievePhanHoiViewModel(DataTransferViewModel dataTransfer)
        {
            List<PhanHoi> lst = null;
            int totalitem = -1;
            if (dataTransfer.IDHopThu != -1)
            {
                lst = _repPhanHoi.SelectByIDHopThu(dataTransfer.IDHopThu);
                totalitem = _repPhanHoi.GetTotalItemByIDHopThu(dataTransfer.IDHopThu);
            }
            else
            {
                lst = _repPhanHoi.SelectAll();
                totalitem = lst.Count;
            }
            return new PhanHoiViewModel
            {
                GetModel = new GetPhanHoiViewModel
                {
                    LstObjModel = lst,
                    TotalItem = totalitem,
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddPhanHoiViewModel
                {
                },
                EditModel = new EditPhanHoiViewModel
                {
                    ID = dataTransfer.IntID,
                },
                ReferKeys = new PhanHoiReferKeys()
                {
                    IDHopThu = dataTransfer.IDHopThu,
                },
                InfoText = dataTransfer.InfoText
            };
        }

        public PhanHoiViewModel RetrieveDetailOfPhanHoiViewModel(DataTransferViewModel dataTransfer)
        {
            List<PhanHoi> lst = null;
            int totalitem = -1;

            lst = _repPhanHoi.RetrieveByID(dataTransfer.IntID);
            totalitem = lst.Count;

            return new PhanHoiViewModel
            {
                GetModel = new GetPhanHoiViewModel
                {
                    LstObjModel = lst,
                    TotalItem = totalitem,
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddPhanHoiViewModel
                {
                },
                EditModel = new EditPhanHoiViewModel
                {
                    ID = dataTransfer.IntID,
                },
                ReferKeys = new PhanHoiReferKeys()
                {
                    IDHopThu = dataTransfer.IDHopThu,
                },
                InfoText = dataTransfer.InfoText
            };
        }

        public LoaiTinTucViewModel RetrieveLoaiTinTucViewModel(DataTransferViewModel dataTransfer)
        {
            return new LoaiTinTucViewModel
            {
                GetModel = new GetLoaiTinTucViewModel
                {
                    LstObjModel = _repLoaiTinTuc.SelectAll(),
                    TotalItem = _repLoaiTinTuc.GetTotalItem(),
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddLoaiTinTucViewModel
                {
                },
                EditModel = new EditLoaiTinTucViewModel
                {
                    ID = dataTransfer.IntID,
                },
                InfoText = dataTransfer.InfoText
            };
        }

        public LoaiTinTucViewModel RetrieveDetailOfLoaiTinTucViewModel(DataTransferViewModel dataTransfer)
        {
            var lst = _repLoaiTinTuc.RetrieveByID(dataTransfer.IDLoaiTinTuc);
            return new LoaiTinTucViewModel
            {
                GetModel = new GetLoaiTinTucViewModel
                {
                    LstObjModel = lst,
                    TotalItem = lst.Count,
                    CurrentPage = 1
                },
                AddModel = new AddLoaiTinTucViewModel
                {
                },
                EditModel = new EditLoaiTinTucViewModel
                {
                    ID = dataTransfer.IntID,
                },
                InfoText = dataTransfer.InfoText
            };
        }

        public LoaiSachViewModel RetrieveDetailOfLoaiSachViewModel(DataTransferViewModel dataTransfer)
        {
            var lst = _repLoaiSach.RetrieveByID(dataTransfer.IDLoaiSach);
            return new LoaiSachViewModel
            {
                GetModel = new GetLoaiSachViewModel
                {
                    LstObjModel = lst,
                    TotalItem = lst.Count,
                    CurrentPage = 1
                },
                AddModel = new AddLoaiSachViewModel
                {
                },
                EditModel = new EditLoaiSachViewModel
                {
                    ID = dataTransfer.IntID,
                },
                InfoText = dataTransfer.InfoText
            };
        }

        public LoaiSachViewModel RetrieveLoaiSachViewModel(DataTransferViewModel dataTransfer)
        {
            return new LoaiSachViewModel
            {
                GetModel = new GetLoaiSachViewModel
                {
                    LstObjModel = _repLoaiSach.SelectAll(),
                    TotalItem = _repLoaiSach.GetTotalItem(),
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddLoaiSachViewModel
                {
                },
                EditModel = new EditLoaiSachViewModel
                {
                    ID = dataTransfer.IntID,
                },
                InfoText = dataTransfer.InfoText
            };
        }

        public HopThuViewModel RetrieveHopThuViewModel(DataTransferViewModel dataTransfer)
        {
            return new HopThuViewModel
            {
                GetModel = new GetHopThuViewModel
                {
                    LstObjModel = _repHopThu.SelectAll(),
                    TotalItem = _repHopThu.GetTotalItem(),
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddHopThuViewModel
                {
                },
                EditModel = new EditHopThuViewModel
                {
                    ID = dataTransfer.IntID,
                },
                InfoText = dataTransfer.InfoText
            };
        }

        public GocChiaSeViewModel RetrieveGocChiaSeViewModel(DataTransferViewModel dataTransfer)
        {
            return new GocChiaSeViewModel
            {
                GetModel = new GetGocChiaSeViewModel
                {
                    LstObjModel = _repGocChiaSe.SelectAll(),
                    TotalItem = _repGocChiaSe.GetTotalItem(),
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddGocChiaSeViewModel
                {
                },
                EditModel = new EditGocChiaSeViewModel
                {
                    ID = dataTransfer.IntID,
                },
                InfoText = dataTransfer.InfoText
            };
        }

        public DoiTacViewModel RetrieveDoiTacViewModel(DataTransferViewModel dataTransfer)
        {
            return new DoiTacViewModel
            {
                GetModel = new GetDoiTacViewModel
                {
                    LstObjModel = _repDoiTac.SelectAll(),
                    TotalItem = _repDoiTac.GetTotalItem(),
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddDoiTacViewModel
                {
                },
                EditModel = new EditDoiTacViewModel
                {
                    ID = dataTransfer.IntID,
                },
                InfoText = dataTransfer.InfoText
            };
        }

        public DanhGiaViewModel RetrieveDanhGiaViewModel(DataTransferViewModel dataTransfer)
        {
            List<DanhGia> lst = null;
            int totalitem = -1;
            if (dataTransfer.IDSach != -1)
            {
                lst = _repDanhGia.SelectByIDSach(dataTransfer.IDSach);
                totalitem = _repDanhGia.GetTotalItemByIDSach(dataTransfer.IDSach);
            }
            else
            {
                lst = _repDanhGia.SelectAll();
                totalitem = lst.Count;
            }
            return new DanhGiaViewModel
            {
                GetModel = new GetDanhGiaViewModel
                {
                    LstObjModel = lst,
                    TotalItem = totalitem,
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddDanhGiaViewModel
                {
                },
                EditModel = new EditDanhGiaViewModel
                {
                    ID = dataTransfer.IntID,
                },
                ReferKeys = new DanhGiaReferKeys()
                {
                    IDSach = dataTransfer.IDSach,
                },
                InfoText = dataTransfer.InfoText
            };
        }


        public NguoiDungViewModel RetrieveNguoiDungViewModel(DataTransferViewModel dataTransfer)
        {
            return new NguoiDungViewModel
            {
                GetModel = new GetNguoiDungViewModel
                {
                    LstObjModel = _repNguoiDung.SelectPaging(dataTransfer.CurrentPage, WebConfiguration.NumNguoiDungPerPage, dataTransfer.Role),
                    TotalItem = _repNguoiDung.GetTotalItem(dataTransfer.Role),
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddNguoiDungViewModel
                {
                },
                EditModel = new EditNguoiDungViewModel
                {
                    ID = dataTransfer.GuidID,
                }
            };
        }

        public NguoiDungViewModel RetrieveDetailOfNguoiDungViewModel(DataTransferViewModel dataTransfer)
        {
            return new NguoiDungViewModel
            {
                GetModel = new GetNguoiDungViewModel
                {
                    LstObjModel = _repNguoiDung.RetrieveByID(dataTransfer.GuidID)
                },
                EditModel = new EditNguoiDungViewModel
                {
                    ID = dataTransfer.GuidID,
                },
                InfoText = dataTransfer.InfoText
            };
        }
        public NguoiDungViewModel RetrieveDetailOfNguoiDungByUserNameViewModel(DataTransferViewModel dataTransfer)
        {
            return new NguoiDungViewModel
            {
                GetModel = new GetNguoiDungViewModel
                {
                    LstObjModel = new List<NguoiDungInfo>
                    {
                        _repNguoiDung.SelectByUserName(dataTransfer.UserName)
                    }
                },
                EditModel = new EditNguoiDungViewModel
                {
                    ID = dataTransfer.GuidID,
                },
                InfoText = dataTransfer.InfoText
            };
        }
        public ThongTinCongTyViewModel RetrieveDetailOfThongTinCongTyViewModel(DataTransferViewModel dataTransfer)
        {
            return new ThongTinCongTyViewModel
            {
                GetModel = new GetThongTinCongTyViewModel
                {
                    LstObjModel = _repThongTinCongTy.RetrieveByID(dataTransfer.IntID)
                },
                EditModel = new EditThongTinCongTyViewModel
                {
                    ID = dataTransfer.IntID,
                },
                InfoText = dataTransfer.InfoText
            };
        }
        public TinTucViewModel RetrieveDetailOfTinTucViewModel(DataTransferViewModel dataTransfer)
        {
            return new TinTucViewModel
            {
                GetModel = new GetTinTucViewModel
                {
                    LstObjModel = _repTinTuc.RetrieveByID(dataTransfer.IntID)
                },
                EditModel = new EditTinTucViewModel
                {
                    ID = dataTransfer.IntID,
                },
                InfoText = dataTransfer.InfoText
            };
        }
        public SachViewModel RetrieveDetailOfSachViewModel(DataTransferViewModel dataTransfer)
        {
            return new SachViewModel
            {
                GetModel = new GetSachViewModel
                {
                    LstObjModel = _repSach.RetrieveByID(dataTransfer.IntID)
                },
                EditModel = new EditSachViewModel
                {
                    ID = dataTransfer.IntID,
                },
                InfoText = dataTransfer.InfoText
            };
        }
        public GocChiaSeViewModel RetrieveDetailOfGocChiaSeViewModel(DataTransferViewModel dataTransfer)
        {
            return new GocChiaSeViewModel
            {
                GetModel = new GetGocChiaSeViewModel
                {
                    LstObjModel = _repGocChiaSe.RetrieveByID(dataTransfer.IntID)
                },
                EditModel = new EditGocChiaSeViewModel
                {
                    ID = dataTransfer.IntID,
                },
                InfoText = dataTransfer.InfoText
            };
        }
        public DoiTacViewModel RetrieveDetailOfDoiTacViewModel(DataTransferViewModel dataTransfer)
        {
            return new DoiTacViewModel
            {
                GetModel = new GetDoiTacViewModel
                {
                    LstObjModel = _repDoiTac.RetrieveByID(dataTransfer.IntID)
                },
                EditModel = new EditDoiTacViewModel
                {
                    ID = dataTransfer.IntID,
                },
                InfoText = dataTransfer.InfoText
            };
        }

    }
}